import {    
    GetSameCityConfig,
    SaveIntraCityAddress, 
    OpenSameCityDelivery,
    CloseSameCityDelivery,
    SaveSameCitySupportPay,
    SaveSameCityConfig,
    GetIntraCityAddress
} from '@/services/order/sameCityDeliver';
import {catchException} from '@/utils/utils';
export default {
    namespace: 'sameCityDelivery',

    state: {
        sameCityConfig: {},  
        sameCityAddress:{}     
    },

    effects: {

        // 获取同城配送配置信息
        *GetSameCityConfig({payload, callback}, {call, put}) {
            const data = yield call(GetSameCityConfig, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    yield put({
                        type: 'saveGetSameCityConfig',
                        payload: data.Data||{},
                    });
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })            
        },  
         // 获取同城配送取货地址
         *GetIntraCityAddress({payload, callback}, {call, put}) {
            const data = yield call(GetIntraCityAddress, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    yield put({
                        type: 'saveGetIntraCityAddress',
                        payload: data.Data||{},
                    });
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })            
        },     

        // 保存同城配送取货地址
        *SaveIntraCityAddress({payload, callback}, {call, put}) {
            const data = yield call(SaveIntraCityAddress, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })            
        }, 
        // 开启同城配送
        *OpenSameCityDelivery({payload, callback}, {call, put}) {
            const data = yield call(OpenSameCityDelivery, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })            
        }, 

        *CloseSameCityDelivery({payload, callback}, {call, put}) {
            const data = yield call(CloseSameCityDelivery, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })            
        }, 
        
        *SaveSameCitySupportPay({payload, callback}, {call, put}) {
            const data = yield call(SaveSameCitySupportPay, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })            
        }, 

        *SaveSameCityConfig({payload, callback}, {call, put}) {
            const data = yield call(SaveSameCityConfig, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    callback && callback.success&&callback.success(data);
                } else {
                    callback && callback.error&&callback.error(data)
                }
            })            
        }, 
    },

    reducers: {
        saveGetSameCityConfig(state, {payload}) {
            return {
                ...state,
                sameCityConfig: payload,
            };
        },
        saveGetIntraCityAddress(state, {payload}) {
            return {
                ...state,
                sameCityAddress: payload,
            };
        },
        saveSendAddrList(state, {payload}) {
            return {
                ...state,
                sendAddrList: payload,
            };
        },
        saveOrderDeliverList(state, {payload}) {
            return {
                ...state,
                orderDeliverList: payload,
            };
        },
        savePrintItemConfig(state, {payload}) {
            return {
                ...state,
                printItemConfig: payload,
            };
        },
        savePendingDeliverList(state, {payload}) {
            return {
                ...state,
                pendingDeliverList: payload,
            };
        },
        saveImportLog(state, {payload}) {
            return {
                ...state,
                importLogList: payload,
            };
        },
    }
}
